/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.uhk.ppro.traveler.services;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.springframework.transaction.annotation.Transactional;
import cz.uhk.ppro.traveler.dao.UsersDao;
import cz.uhk.ppro.traveler.entity.TBudget;
import cz.uhk.ppro.traveler.entity.TTransaction;
import cz.uhk.ppro.traveler.entity.TTrip;
import cz.uhk.ppro.traveler.entity.TUser;

/**
 *
 * @author dusi
 */
@Transactional
public class DbInit {

    EntityManagerFactory emf;
    EntityManager em;
    
    @SpringBean("users")
    UsersDao usersDao;

    public void init() {
        
        emf = Persistence.createEntityManagerFactory("pu");
        em = emf.createEntityManager();
        em.getTransaction().begin();
        
        int countUsers = Integer.parseInt(em.createQuery("select count(*) from TUser").getSingleResult().toString());
        
        if (countUsers == 0){
            insertData();
        }
        
        em.getTransaction().commit();
        em.close();
        emf.close();
        
    }
    
    public void insertData(){

        DateFormat sf = new SimpleDateFormat("dd.MM.yyyy");
        
        // User
        TUser u1 = new TUser("drs", "sousek");
        TUser u2 = new TUser("dusi", "dusatko");
        
        u1.setLocation("Opava");
        u2.setLocation("Vancouver");
        
        // Budget
        TBudget b1 = new TBudget(500, 10000, 7000, 2000, u1);
        TBudget b2 = new TBudget(500, 10000, 7000, 2000, u2);
        
        u1.setBudget(b1);
        u2.setBudget(b2);
        
        // Transactions
        Date date1= new Date();
        Date date2= new Date();
        Date date3= new Date();
        Date date4= new Date();
        
        try {
            
            date1 = sf.parse("21.4.2011");
            date2 = sf.parse("22.4.2011");
            date3 = sf.parse("15.4.2011");
            date4 = sf.parse("18.4.2011");
        }
        
        catch (ParseException ex) {
            
            Logger.getLogger(DbInit.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        TTransaction tr1 = new TTransaction(date1,10000, 1, b1);
        TTransaction tr2 = new TTransaction(date2,1000, -1, b1);
        TTransaction tr3 = new TTransaction(date3, 3000, 1, b2);
        TTransaction tr4 = new TTransaction(date4, 10000, -1, b2);

        b1.getTransactions().add(tr1);
        b1.getTransactions().add(tr2);
        b2.getTransactions().add(tr3);
        b2.getTransactions().add(tr4);

        // Trips
        TTrip t1 = new TTrip("Paris", 5000, 1, u1);
        TTrip t2 = new TTrip("Madrid", 20000, 2, u1);
        TTrip t3 = new TTrip("Las Vegas", 75000, 3, u1);
        TTrip t4 = new TTrip("Sydney", 40000, 1, u2);
        TTrip t5 = new TTrip("Prague", 50000, 2, u2);
        TTrip t6 = new TTrip("Las Vegas", 5000, 3, u2);

        u1.getTrips().add(t1);
        u1.getTrips().add(t2);
        u1.getTrips().add(t3);
        u2.getTrips().add(t4);
        u2.getTrips().add(t5);
        u2.getTrips().add(t6);

        // Persist
        em.persist(u1);
        em.persist(u2);
    }

}
